<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">📝</text>
						<text class="tool-title">姓名分析</text>
						<text class="tool-subtitle">姓名五格数理分析</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">姓名信息</text>
						
						<view class="form-item">
							<text class="form-label">姓名</text>
							<input 
								v-model="params.name" 
								placeholder="请输入真实姓名"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">性别</text>
							<view class="gender-selector">
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'male' }"
									@tap="selectGender('male')"
								>
									<text class="gender-icon">👨</text>
									<text class="gender-text">男</text>
								</view>
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'female' }"
									@tap="selectGender('female')"
								>
									<text class="gender-icon">👩</text>
									<text class="gender-text">女</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">出生日期（可选）</text>
							<picker 
								mode="date" 
								:value="params.birthday" 
								@change="onBirthdayChange"
								class="birthday-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayBirthday }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '分析中...' : '开始分析'" 
						type="primary"
						@tap="queryNameAnalysis"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="nameAnalysisResult">
					<view class="result-section">
						<text class="section-title">姓名分析</text>
						
						<view class="name-analysis">
							<view class="overall-name">
								<view class="name-score">
									<view class="score-circle">
										<text class="score-text">{{ nameAnalysisResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ nameAnalysisResult.overall.level }}</text>
								</view>
								<view class="name-desc">
									<text class="desc-text">{{ nameAnalysisResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="name-info">
								<view class="info-item">
									<text class="info-label">姓名</text>
									<text class="info-value">{{ nameAnalysisResult.nameInfo.name }}</text>
								</view>
								<view class="info-item">
									<text class="info-label">笔画数</text>
									<text class="info-value">{{ nameAnalysisResult.nameInfo.strokes.join(' + ') }}</text>
								</view>
								<view class="info-item">
									<text class="info-label">五行属性</text>
									<text class="info-value">{{ nameAnalysisResult.nameInfo.fiveElements.join(' + ') }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 五格分析 -->
				<app-card v-if="nameAnalysisResult && nameAnalysisResult.fiveGrid">
					<view class="five-grid-section">
						<text class="section-title">五格数理</text>
						
						<view class="five-grid-list">
							<view 
								v-for="(grid, gridName) in nameAnalysisResult.fiveGrid" 
								:key="gridName"
								class="grid-item"
							>
								<view class="grid-header">
									<text class="grid-name">{{ getGridName(gridName) }}</text>
									<view class="grid-number">{{ grid.number }}画</view>
									<view class="grid-level" :class="grid.level">
										<text class="level-text">{{ grid.level }}</text>
									</view>
								</view>
								<text class="grid-meaning">{{ grid.meaning }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 性格分析 -->
				<app-card v-if="nameAnalysisResult && nameAnalysisResult.analysis">
					<view class="analysis-section">
						<text class="section-title">性格分析</text>
						
						<view class="analysis-list">
							<view 
								v-for="(analysis, index) in nameAnalysisResult.analysis" 
								:key="index"
								class="analysis-item"
							>
								<view class="analysis-header">
									<text class="analysis-aspect">{{ analysis.aspect }}</text>
								</view>
								<text class="analysis-description">{{ analysis.description }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 建议 -->
				<app-card v-if="nameAnalysisResult && nameAnalysisResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">姓名建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in nameAnalysisResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'
import UserStateManager from '@/utils/user-state-manager.js'

export default {
	name: 'NameAnalysisToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				name: '',
				gender: '',
				birthday: ''
			},
			nameAnalysisResult: null,
			loading: false,
			toolsService: null
		}
	},
	computed: {
		canQuery() {
			return this.params.name.trim().length >= 2 && this.params.gender
		},
		
		displayBirthday() {
			return this.params.birthday || '选择生日'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
				
			// 自动填写用户信息
			this.autoFillUserInfo()
		},
			
		/**
		 * 自动填写用户信息
		 */
		autoFillUserInfo() {
			const autoFillInfo = UserStateManager.getAutoFillInfo()
			if (autoFillInfo) {
				this.params.name = autoFillInfo.name
				this.params.gender = autoFillInfo.gender
				this.params.birthday = autoFillInfo.birthday
					
				// 显示提示
				uni.showToast({
					title: '已自动填入个人信息',
					icon: 'none',
					duration: 2000
				})
			} else {
				// 提示用户登录或完善信息
				setTimeout(() => {
					UserStateManager.promptCompleteInfo()
				}, 1000)
			}
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		selectGender(gender) {
			this.params.gender = gender
		},
		
		onBirthdayChange(e) {
			this.params.birthday = e.detail.value
		},
		
		getGridName(gridName) {
			const names = {
				'tianGe': '天格',
				'renGe': '人格',
				'diGe': '地格',
				'waiGe': '外格',
				'zongGe': '总格'
			}
			return names[gridName] || gridName
		},
		
		async queryNameAnalysis() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请输入姓名并选择性别',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getNameAnalysisInfo(this.params)
				
				if (result.success) {
					this.nameAnalysisResult = result.data
					uni.showToast({
						title: '分析完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '分析失败')
				}
			} catch (error) {
				console.error('姓名分析失败:', error)
				uni.showToast({
					title: '分析失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.gender-selector {
			display: flex;
			gap: 16rpx;
			
			.gender-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #607D8B;
					background: linear-gradient(135deg, #607D8B 0%, #455A64 100%);
					
					.gender-icon,
					.gender-text {
						color: white;
					}
				}
				
				.gender-icon {
					font-size: 40rpx;
					margin-bottom: 8rpx;
				}
				
				.gender-text {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
		
		.birthday-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.name-analysis {
		.overall-name {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #607D8B 0%, #455A64 100%);
			border-radius: 16rpx;
			
			.name-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.name-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.name-info {
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.info-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 16rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.info-label {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.7);
				}
				
				.info-value {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
			}
		}
	}
}

.five-grid-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.five-grid-list {
		.grid-item {
			margin-bottom: 20rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.grid-header {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 12rpx;
				
				.grid-name {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				
				.grid-number {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.7);
				}
				
				.grid-level {
					padding: 4rpx 12rpx;
					border-radius: 12rpx;
					
					&.ji {
						background-color: #4CAF50;
					}
					
					&.xiong {
						background-color: #F44336;
					}
					
					&.banji {
						background-color: #FF9800;
					}
					
					.level-text {
						font-size: 20rpx;
						color: white;
						font-weight: 500;
					}
				}
			}
			
			.grid-meaning {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.analysis-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.analysis-list {
		.analysis-item {
			margin-bottom: 20rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.analysis-header {
				margin-bottom: 12rpx;
				
				.analysis-aspect {
					font-size: 28rpx;
					font-weight: 500;
					color: #607D8B;
				}
			}
			
			.analysis-description {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}
</style>